/* ParentBehavior.cpp - implementation of ParentBehavior class */



#include "ParentBehavior.h"

void ParentBehavior::Execute()
{
        if(control->Finished())
        {
			//Moving through sequence.  If we get to end, bail.
                return;
        }
        if(control->GetCurrentBehavior() == NULL)
        {
                // If there is a null task, we've done something wrong
                return;
        }
        
        // If we do have a curTask...
        if( !control->GetCurrentBehavior()->GetControl()->Started())
        {
                // ... and it's not started yet, start it.
                control->GetCurrentBehavior()->GetControl()->SafeEnter();
        }               
        else if(control->GetCurrentBehavior()->GetControl()->Finished())
        {
                // ... and it's finished, end it properly.
                control->GetCurrentBehavior()->GetControl()->SafeExit();
                
                if(control->GetCurrentBehavior()->GetControl()->Succeeded())
                {
					BehaviorSucceeded();
                }
                if(control->GetCurrentBehavior()->GetControl()->Failed())
                {
					BehaviorFailed();
                }
        }
        else
        {               
                // ... and it's ready, update it.               
                control->GetCurrentBehavior()->Execute();
        }       
}

void ParentBehavior::Enter() 
{
        control->SetCurrent( 0 );
        if(control->GetCurrentBehavior() == NULL)
        {
                //No behavior for this control.  Shouldn't happen.
        }
}